package com.campusnews.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.campusnews.annotation.LoginToken;
import com.campusnews.pojo.Invitation;
import com.campusnews.pojo.Build;
import com.campusnews.pojo.Location;
import com.campusnews.pojo.Thing;
import com.campusnews.service.BuildService;
import com.campusnews.service.InvitationService;
import com.campusnews.service.BuildService;
import com.campusnews.service.TopicService;
import com.campusnews.utils.EntryToMapUtils;
import com.campusnews.utils.JsonResult;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lqc
 * @since 2021-09-26
 */
@RestController
@RequestMapping("/Build")
@Api(tags={"地点API"})
public class BuildController {

    @Autowired
    private BuildService BuildService;

    @LoginToken
    @ApiOperation(value = "添加地点信息", notes = "添加地点信息")
    @RequestMapping(value = "/insertBuild" ,method = RequestMethod.POST)
    public JsonResult insertBuild(@RequestBody Build build) {
        QueryWrapper<Build> wrapper = new QueryWrapper<>();
        wrapper.eq("x_position",build.getXPosition())
                .eq("y_position",build.getYPosition());
        Build buildBase = BuildService.getOne(wrapper);
        if (buildBase!=null){
            return JsonResult.errorMsg("地点已存在");
        }else {
            boolean save = BuildService.save(build);
            if (save){
                System.out.println(build);
                return JsonResult.build(200,"地点添加成功",build);
            }else {
                return JsonResult.errorMsg("地点添加失败");
            }
        }
    }

    @LoginToken
    @ApiOperation(value = "修改地点信息", notes = "修改地点信息")
    @RequestMapping(value = "/updateBuild" ,method = RequestMethod.POST)
    public JsonResult updateBuild(@RequestBody Build build) {
        boolean update = BuildService.updateById(build);
        if (update){
            return JsonResult.build(200,"地点修改成功",build);
        }else {
            return JsonResult.errorMsg("地点修改失败");
        }
    }

    @LoginToken
    @ApiOperation(value = "删除地点信息", notes = "删除地点信息")
    @RequestMapping(value = "/deleteBuild/{buildId}" ,method = RequestMethod.GET)
    public JsonResult deleteBuild(@PathVariable("buildId") int buildId) {
        QueryWrapper<Build> wrapper = new QueryWrapper<>();
        wrapper.eq("build_id",buildId);
        boolean remove = BuildService.remove(wrapper);
        if (remove){
            return JsonResult.build(200,"地点删除成功",buildId);
        }else {
            return JsonResult.errorMsg("地点删除失败");
        }
    }

    @LoginToken
    @ApiOperation(value = "分页精确查询地点信息", notes = "分页精确地点信息")
    @RequestMapping(value = "/selectBuildByAccurate" ,method = RequestMethod.POST)
    public JsonResult selectBuildByAccurate(@RequestBody Build build) {
        Map<String, Object> map = new HashMap<>();
        try {
            map = EntryToMapUtils.entryToMap(build);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        //条件查询器
        QueryWrapper<Build> wrapper = new QueryWrapper<>();
        wrapper.allEq(map);
        if (!(build.getSortOrder()==null||build.getSortOrder().length()==0)){
            wrapper.orderByDesc(build.getSortOrder());
        }
        PageHelper.startPage(build.getPage(),build.getPageSize());
        PageInfo<Build> pageInfo = new PageInfo<>((Page<Build>)BuildService.list(wrapper));
        System.out.println(pageInfo);
        return JsonResult.build(200,"分页精确地点信息查询成功",pageInfo);
    }

    @LoginToken
    @ApiOperation(value = "分页模糊查询地点信息", notes = "分页模糊地点信息")
    @RequestMapping(value = "/selectBuildByVague" ,method = RequestMethod.POST)
    public JsonResult selectBuildByVague(@RequestBody Thing things) {
        //条件查询器
        QueryWrapper<Build> wrapper = new QueryWrapper<>();
        String thing = things.getThing();
        if (!(thing==null||thing.length()==0)){
            wrapper.like("build_name",thing);
        }
        if(StringUtils.isNumeric(thing)){
            wrapper.like("invi_id",Integer.valueOf(thing)).or()
                    .like("invi_num",Integer.valueOf(thing));

        }
        if (!(things.getSortOrder()==null||things.getSortOrder().length()==0)){
            wrapper.orderByDesc(things.getSortOrder());
        }
        PageHelper.startPage(things.getPage(),things.getPageSize());
        PageInfo<Build> pageInfo = new PageInfo<>((Page<Build>)BuildService.list(wrapper));
        System.out.println(pageInfo);
        return JsonResult.build(200,"分页模糊地点信息查询成功",pageInfo);
    }
}

